<?php
/*数据库链接封装*/
    function db_connect($db_name,$host='localhost',$db_user='root',$db_pwd='root',$charset='utf8'){

    $link = mysqli_connect($host,$db_user,$db_pwd,$db_name);

    if(!$link){
        echo '数据库链接失败！';
    }
    mysqli_query($link,'set names '.$charset);

    return $link;

    }
/*删除操作封装*/
    function getDel($table,$where){

        if (is_array($where)) {

            $where = 'in ('.implode(',',$where).')';

        }else{

            $where = "=$where";
        }
        //print_r($GLOBALS);die;
        //echo $where;die;

        $sql = "delete from `$table` where `id` $where";
        mysqli_query($GLOBALS['link'],$sql);
        $rows = mysqli_affected_rows($GLOBALS['link']);

        return $rows;

    }

/*搜索一条记录封装*/
    function getSelbyOne($table,$where,$show='*'){

        if (is_array($show)) {

            $show = implode(',',$show);

        }
        $data = array();
        $sql = "select $show from `$table` where `id` = '$where'" ;
        $result = mysqli_query($GLOBALS['link'],$sql);
        $data = mysqli_fetch_assoc($result);

        return $data;
    }

/*修改封装*/
    function getUpdate($table,$term,$where,$upda){

        foreach ($upda as $k => $v) {
            $con[] = '`'.$k.'`='."'".$v."'";
        }

        $con = implode(',',$con);

        $sql = "update `$table` set $con where `$term`='$where'";//echo $sql;die;

        mysqli_query($GLOBALS['link'],$sql);
        $rows = mysqli_affected_rows($GLOBALS['link']);

        return $rows;

    }

/*添加封装*/
    function getInsert($table,$ins){

        $name = array_keys($ins);
        $name = '(`'.implode('`,`',$name).'`)';

        $val = array_values($ins);
        $val = "('".implode("','",$val)."')";

        $sql = "insert into `$table` $name values $val";
        mysqli_query($GLOBALS['link'],$sql);
        $rows = mysqli_affected_rows($GLOBALS['link']);
        return $rows;

    }

/*搜索封装*/
    function getSelect($table,$where=1,$limit='',$order='',$show='*'){

        if (is_array($show)) {
            $show = implode(',',$show);
        }
        if (!empty($limit)) {
            $limit = "limit $limit";
        }
        if(!empty($order)){
            $order = 'order by'.$order;
        }
        $data = array();
        $sql = "select $show from `$table` where $where $order $limit";
        $result = mysqli_query($GLOBALS['link'],$sql);

        while ($arr = mysqli_fetch_assoc($result)) {

            $data[] = $arr;

        }

        return $data;
    }

?>